package com.myblog.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.myblog.mapper.TagMapper;
import com.myblog.model.Tag;
import com.myblog.service.TagService;
import com.myblog.utils.DataMap;
import com.myblog.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TagServiceImpl implements TagService {

    @Autowired
    private TagMapper tagMapper;

    /**
     * 插入标签
     * @param newArticleTags
     * @param tagSize
     */
    @Override
    public void insertTag(String[] newArticleTags, int tagSize) {
        //插入之前去重
        for (String newArticleTag : newArticleTags) {
            if(tagMapper.selectByTagName(newArticleTag) == null){
                Tag tag = new Tag(newArticleTag,tagSize);
                tagMapper.insertTag(tag);
            }
        }
        return;
    }

    @Override
    public DataMap getTagsCloud() {
        List<Tag> tags = tagMapper.selectAllTags();

        JSONArray jsonArray = new JSONArray();
        for (Tag tag : tags) {
            JSONObject tagJson = new JSONObject();
            tagJson.put("tagName",tag.getTagName());
            tagJson.put("tagSize",tag.getTagSize());

            jsonArray.add(tagJson);
        }

        JSONObject result = new JSONObject();
        result.put("result",jsonArray);
        return DataMap.success().setData(result);
    }
}
